<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin\Sys;

use App\Controller\AbstractController;
use App\Middleware\AuthMiddleware;
use App\Model\Dict;
use App\Model\DictDatum;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middlewares([AuthMiddleware::class])]
#[Controller]
class DictDataController extends AbstractController
{
    #[GetMapping(path: '/sys/dictData/list')]
    public function list()
    {
        $pageNum = $this->request->input('pageNum', 1);
        $pageSize = $this->request->input('pageSize', 10);
        $dictId = $this->request->input('dictId', '');
        $list = DictDatum::query()->where('dict_id', $dictId)->forPage($pageNum, $pageSize)->get();
        $data['searchCount'] = true;
        $data['current'] = (int)$pageNum;
        $data['size'] = (int)$pageSize;
        $data['total'] = DictDatum::query()->where('dict_id', $dictId)->count();
        $data['pages'] = ceil($data['total'] / $pageSize);
        $data['records'] = $list;
        return success($data);
    }

    #[RequestMapping(path: '/sys/dictData/add', methods: ['POST'])]
    public function add()
    {
        $dictData = $this->request->inputs(['dictId', 'label', 'status', 'remark', 'sort', 'style', 'value']);
        $dictData['dict_id'] = $dictData['dictId'];
        unset($dictData['dictId']);
        try {
            DictDatum::query()->create($dictData);
            return success();
        } catch (\Throwable $e) {
            return fail(201, $e->getMessage());
        }
    }

    #[RequestMapping(path: '/sys/dictData/update', methods: ['PUT'])]
    public function update()
    {
        $dictData = $this->request->inputs(['id', 'label', 'status', 'remark', 'sort', 'style', 'value']);
        try {
            DictDatum::query()->updateOrInsert(['id' => $dictData['id']], $dictData);
            return success();
        } catch (\Throwable $e) {
            return fail(201, $e->getMessage());
        }
    }

    #[DeleteMapping(path: '/sys/dictData/delete/{ids}')]
    public function delete($ids)
    {
        try {
            $idArr = explode(',', $ids);
            DictDatum::query()->whereIn('id', $idArr)->delete();
            return success();
        } catch (\Throwable $e) {
            Db::rollBack();
            logInfo('delete', $e->getMessage(), 'dictData');
            return fail(201, $e->getMessage());
        }

    }

    #[GetMapping(path: '/sys/dictData/getDiceData/{type}')]
    public function article_status($type)
    {
        $typeArr = explode(',', $type);
        $list = Dict::query()->whereIn('type', $typeArr)->with(['dictData'])->get();
        // return success($list);
        $data = [];
        foreach ($typeArr as $type_item) {
            foreach ($list->toArray() as $item) {
                if ($type_item == $item['type']) {
                    $data[$item['type']]['list'] = $item['dict_data'];
                    $data[$item['type']]['defaultValue'] = null;
                }
            }
        }
        return success($data);
    }


}
